﻿{$header}
<div id="main" class="main" >
    <div class="content">
  <h3 class="heading">批发销售单 [ <a href="__URL__">返回列表</a> ]    <a class="btn btn-primary" href="__APP__/Wholesale/print_sale/id/{$vo.id}">打印出库单</a> </h3>

<div id="result" class="result none"></div>
<table cellpadding=3 cellspacing=3 >
    <form class="form-horizontal well" method='post' id="form1" action="__URL__/update/">

    <tr><td colspan="4" class="heading h4 alert alert-info">基本信息</td> </tr>
    <tr>
    	<td class="tRight" >订单号：</td>
        <td class="tLeft" >{$vo.wholesale_no} </td>
        <td>订单状态：</td><td>{$vo.order_status|getConfigVal='orderstatus'}</td>
    </tr>
    <tr>
        <td>客户：</td><td>
            <?php   $customers = M('Customer')->where('type = 5')->field('id,name')->findAll(); ?>
            订单来源{:configList(C('wholesale_source'), $vo['wholesale_source_id'],'wholesale_source_id' )}
            <select name="customer_id">
                <volist name="customers" id="cu">
                    <option value="{$cu.id}" <eq name="vo.customer_id" value="$cu.id">selected</eq> >{$cu.name}</option>
                </volist>
                
            </select>
        </td>
        <td>创建时间：</td><td>{$vo.create_time|toDate}</td>
    </tr>
    <tr>
        <td>应收款：</td><td>{$vo.should_pay} , 货款：{$vo.total} <!--暂用sum_pay代替real_pay--></td>
        <td>快递费：</td><td> <input type="text" name="logifee" value="{$vo.logifee}"> </td>
    </tr>
    <tr>
        <td>快递公司：</td><td>{:modList('Logicompany',$vo['logicompany_id'],'span2','logicompany_id','sort desc' )}</td>
        <td>单号：</td><td> <input type="text" class="input-xxlarge" name="logino" value="{$vo.logino}">  发货时间: {$vo.shipping_time|toDate} </td>
    </tr>
    
     <tr><td colspan="4" class="heading h4 alert alert-info">收货信息</td> </tr>
     <tr> <td>收货人：</td><td>
        <input type="hidden" name="shipping_address_id" value="{$vo.shipping_address_id}" >
        <input type="text" name="shipping_name" value="{$vo.shipping_name}"> 
    </td>
         <td>手机</td><td> <input type="text" name="mobile" value="{$vo.mobile}"> </td>
        
    </tr>
    <tr>
       <td>地址：</td><td><input type="text" class="input-xxlarge" name="address" value="{$vo.address}"> </td>
        <td>电话</td><td> <input type="text" name="phone" value="{$vo.phone}"> </td>
    </tr>
    

<tr>
	
        <!--
            <select name="user_id">
                <volist name="users" id="us">
                <option value="{$key}" <eq name="vo.user_id" value="$key">selected</eq> >{$us}</option>
                </volist>
            </select> -->
            <!--如果审核通过，则不能再返回待审核状态-->
            <input type="hidden" name="status" value="1">
    
    <td>备注</td>
    <td><textarea name="remark" rows="2" class="span6">{$vo.remark}</textarea> </td>
</tr>

<tr>
	<td></td>
	<td class="center" width="300">
        <input type="hidden" value="{$vo.id}" name="id">
        <eq name="vo.status" value="0">
            <input type="hidden" value="1" name="status"> <!--审核通过，并待发货-->
             <input type="hidden" name="check_time" value="{:time()}">
             <input type="hidden" name="shipping_time" value="{:time()}">
             <input type="hidden" name="order_status" value="3"> <!--shipping-->
            <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i>审核通过，发货</button>

           <else/>
                <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i>修改</button>
        </eq>
	<button type="reset" onclick="javascript:history.back()" class="btn btn-primary offset1"><i class="icon-arrow-left icon-white"></i> 返回</button>
	</td>
</tr>
</form>
<tr><td colspan="3" class="heading h4 alert alert-info">商品明细--{$vo.sale_no} ( 总数量：{$vo.sum_number})</td> 
        <td>
            <lt name="vo.order_status" value="3">
                <a href="__APP__/Wholesale_detail/add/id/{$vo.id}" class="btn btn-primary">增录新品</a>
            </lt>
        
        </td>
        
    </tr>
    <tr>
        <td colspan="4">
            <table class="table table-bordered table-striped" id="sale_detail">
                <tr>
                    <th>序号</th><th>商品名称</th><th class="span2">规格</th><th>价格</th><th>数量</th><th class="span1">单位</th><th>小计</th><th>当前库存</th><th>修改</th><th>删除</th>
                </tr>
                <php>$sale_details = M('Wholesale_detail')->where('wholesale_id ="'.$vo['id'].'"')->order('id')->findAll(); </php>
                <volist name="sale_details" id="sa">
                    <form action="__APP__/Wholesale_detail/update" method="post">
                    <tr>
                        <td>{$key+1} <input type="hidden" name="id" value="{$sa.id}"> 
                            <input type="hidden" name="update_time" value="{:time()}">
                         </td>
                        <td> {$sa.product_id|getModelName='Product'}</td>
                        <td>
                           {$sa.product_id|printGoodsOption} 
                           <eq name="sa.is_gift" value="1"><img src="__PUBLIC__/Images/gift.jpg" width="40" height="40"> </eq> 
                        </td>
                        <td>
                            <input type="text" name="price" class="span1" value="{$sa.price}">
                        </td>
                        <td><input type="text" name="number" value="{$sa.number}" class="span1" > </td>
                        <td>{$sa.product_id|getProductUnit}</td>
                        <td>{$sa.total}</td>
                        <td>{$sa.product_id|getDStock}</td>
                        <lt name="vo.order_status" value="3">
                            <td> 
                                <input type="hidden" name="jumpUrl" value="__URL__/edit/id/{$vo.id}">
                                <input type="submit" value="修改" class ="btn"> 
                            </td>
                            <td><a href="__APP__/Wholesale_detail/foreverdelete/id/{$sa.id}" class="btn">删除</a>  </td>
                        </lt>
                    </tr>
                    </form>
                </volist>
            </table>
        </td>
    </tr>
    
</table>
<?php 
$action_history = M('Action')->where('model ="Wholesale" and obj_id ='.$vo['id'] )->order('id desc')->findAll(); 
$sale_detail_ids = M('Wholesale_detail')->where('wholesale_id ='.$vo['id'])->field('id')->findAll(); //not include delete sale_detail
$detail_history = array();
$delete_sale_detail_ids = array() ;


$delete_sale_details = D('ActionHistoryView')->where('model = "Wholesale_detail" and field ="delete"')->field('id,diff')->findAll();
$this_del_details = array();
foreach ($delete_sale_details as $dkey => $dvalue) {
    $detail = unserialize($dvalue['diff']);
    if($detail[0]['wholesale_id'] == $vo['id'] ){ //得到对应的删除ACTION
       $delete_sale_detail_ids[$dkey]['id'] = $detail[0]['id']; //存入已经删除的sale_detail_id
    }
}
//the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended
$sale_detail_ids = array_merge($sale_detail_ids, $delete_sale_detail_ids );
foreach ($sale_detail_ids as $k => $v) {

    $tmp_detail = M('Action')->where('model ="Wholesale_detail" and obj_id ='.$v['id'] )->order('id desc')->findAll();
    
    if(is_array($tmp_detail))
       $detail_history = array_merge($detail_history,$tmp_detail );
    
}

?>
<table class="table table-bordered table-striped">
    <tr><td colspan="7" class="heading h4 alert alert-info">操作日志 </td></tr>
<tr><th>序号</th><th>操作人</th><th>ip</th><th>操作时间</th><th>操作概要</th> <th>动作</th> <th>详细</th> </tr>
<volist name="action_history" id="ah"> 
    <tr>
        <td>{$ah.id}</td>
        <td>{$ah.user_id|getModelName='User'}</td>
        <td>{$ah.ip}</td>
        <td>{$ah.create_time|toDate}</td>
        <td>{$ah.comment}</td>
        <td>{$ah.action}</td>
        <?php 
        $historys = M('History')->where('action_id ='.$ah['id'])->findAll();
         ?>
        <td>
            <div class="changes" id="changeBox3" style="display: block; ">{:A('Action')->printChanges('Sale',$historys)}</div>
        </td>
    </tr>
</volist>

<volist name="detail_history" id="dh"> 
    <tr>
        <td>{$dh.id}</td>
        <td>{$dh.user_id|getModelName='User'}</td>
        <td>{$dh.ip}</td>
        <td>{$dh.create_time|toDate}</td>
        <td>{$dh.comment}</td>
        <td>{$dh.action}</td>
        <?php 
        $historys = M('History')->where('action_id ='.$dh['id'])->findAll();
         ?>
        <td>
            <div class="changes" id="changeBox3" style="display: block; ">
                <eq name="dh.action" value="删除">
                    {:dump(unserialize($historys[0]['diff']))}
                    <else/>
                    {:A('Action')->printChanges('Sale_detail',$historys)}
                </eq>
            </div>
        </td>
    </tr>
</volist>

</table>

</div>
</div>
<include file="Public:footer" />
<link rel="stylesheet" type="text/css" href="__PUBLIC__/Js/easyui/themes/default/easyui.css">
<script type="text/javascript" src="__PUBLIC__/Js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/Js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/Js/filter-product.js"></script>
<script type="text/javascript">
    function copy_sale_detail (id) {
        var sale_detail_id = id;
        if( sale_detail_id > 0){
        jQuery.post(
        APP+'/Wholesale_detail/getSaleDetail',
        {   sale_detail_id:sale_detail_id
        },
        function(data){
            data = data.data;
            $('#sale_detail').append(data.sale_detail);
        },
        'json'
        );
        }
    }
</script>
